package cn.bopomo.dealer.base.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.ModuleForm;
import cn.bopomo.dealer.base.vo.ModuleList;

public class ModuleAddAction extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		
		if (!this.hasPermission(request)){
			//error
			return this.findFailure(mapping);
		}
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		
		
		ModuleList module = new ModuleList();
		
		BeanUtils.copyProperties(module,(ModuleForm)form);
		
		
		module.setAddDate(new Date());
		
		module.setAddUid(Integer.parseInt(this.getUserId(request)));
		
		String result = os.checkModuleValidate(module);
		
		if (!result.equals(ResultCode.OP_SUCCESS)){
			
			return this.findFailure(mapping);
		}
		
		result = os.addModule(module);
		
		if (!result.equals(ResultCode.OP_SUCCESS)){
			
			return this.findFailure(mapping);
		}
		
		return this.findSuccess(mapping);
	}

}
